#include "CubeMapShadowMapFrameBuffer.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>

namespace OCPP
{
	CubeMapShadowMapFrameBuffer::CubeMapShadowMapFrameBuffer(int width,int height)
	{
		CubeMapShadowMapFrameBuffer::width = width;
		CubeMapShadowMapFrameBuffer::height = height;

		// Framebuffer for Shadow Map
		glGenFramebuffers(1, &ID);

		// Texture for Shadow Map FBO
		
		glGenTextures(1, &shadowMap);
		glBindTexture(GL_TEXTURE_CUBE_MAP, shadowMap);

		for (unsigned int i = 0; i < 6; ++i)
			glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT,
				width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);

		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

		glBindFramebuffer(GL_FRAMEBUFFER, ID);
		glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, shadowMap, 0);
		// Needed since we don't touch the color buffer
		glDrawBuffer(GL_NONE);
		glReadBuffer(GL_NONE);
		glBindFramebuffer(GL_FRAMEBUFFER, 0);
	}

	CubeMapShadowMapFrameBuffer::~CubeMapShadowMapFrameBuffer()
	{

	}

	void CubeMapShadowMapFrameBuffer::bind()
	{
		glViewport(0, 0, width, height);
		glBindFramebuffer(GL_FRAMEBUFFER, ID);
	}

	void CubeMapShadowMapFrameBuffer::unbind()
	{
		glBindFramebuffer(GL_FRAMEBUFFER, 0);
	}

	void CubeMapShadowMapFrameBuffer::draw()
	{

	}
}